## Script (Python) "upd_eleves.coord.cpy"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=Update user record and Send message to manager
##
# -*- coding: utf-8 -*-

from Products.CMFCore.utils import getToolByName
from Products.CMFPlone import PloneMessageFactory as _

# Vérifier que le MailHost est correctement configuré
mailhost = getToolByName(context, 'MailHost')

if mailhost is None:
   raise AttributeError, "Cannot find a Mail Host object"


session=context.REQUEST.SESSION

# Get a value from the REQUEST
tel = context.REQUEST.get('tel')
tel2 = context.REQUEST.get('tel2')
tel3 = context.REQUEST.get('tel3')
tel4 = context.REQUEST.get('tel4')
email = context.REQUEST.get('email')

# maj de la fiche élève
container.upd_eleves_coordonnees(cd_cli=session['matricule'], 
                              tel=tel,
                              tel2=tel2,
                              tel3=tel3,
                              tel4=tel4,
                              email=email)
# maj la variable de session
session['email']=email
session['nomail']="0"

# lire l'adresse email de l'agence de l'élève
agence_email = container.get_eleve_agence_email(cd_cli=session['matricule'])[0]
# envoi d'un message avertissant la secrétaire
mBody = u"<p>Bonjour,</p>"
mBody += u"<p>%s<br />Code élève : %s</p>" % (session['nom'], session['matricule'])
mBody += u"<p>vient de changer ses coordonnées via l'espace client de marietton.com.</p>"
mBody += u"<p>Cordialement,</p>"
mBody += u"<p>Espace Client MARIETTON</p>"

mTo = agence_email.email
mFrom = email
mSubj = '[monespace] Changement de coordonnées'
mailhost.send(mBody, mto=mTo, mfrom=mFrom, subject=mSubj, msg_type='text/html', charset='utf-8')                              
# No errors.  Always make sure to return the ControllerState object
state.set(portal_status_message="Vos nouvelles coordonnées ont été enregistrées")

# set the default next action 
state.setNextAction('redirect_to:string:main_menu')

return state
